package cn.car91.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.adapter.WeizhangResponseAdapter;
import cn.car91.adapter.WeizhangResponseAdapter.WeizhangResponseHistoryJson;
import cn.car91.utils.RegexUtil;
import cn.car91.view.HerilyAlertDialog;
import cn.car91.view.MyListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.thinkland.sdk.android.DataCallBack;
import com.thinkland.sdk.android.JuheData;
import com.thinkland.sdk.android.Parameters;

public class IllegalResultActivity extends BaseActivity {
    final String queryUrl = "http://v.juhe.cn/wz/query";

    @ViewInject(R.id.result_list)
    MyListView resultLstView;
    @ViewInject(R.id.query_chepai)
    TextView chepaiView;
    @ViewInject(R.id.query_city)
    TextView cityView;
    @ViewInject(R.id.result_title)
    TextView descView;
    @ViewInject(R.id.result_null)
    TextView nullView;
    @ViewInject(R.id.popLoader)
    FrameLayout loaderView;

    WeizhangResponseAdapter weizhangResponseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_illegal_result);
        super.onCreate(savedInstanceState);
        weizhangResponseAdapter = new WeizhangResponseAdapter(this, new ArrayList());
        resultLstView.setAdapter(weizhangResponseAdapter);

        getData();
        ((TextView) findViewById(R.id.header_text)).setText("违章记录");
    }

    private void getData() {
        String paramsStr = getIntent().getStringExtra("params");
        loaderView.setVisibility(View.VISIBLE);
        ((View) resultLstView.getParent()).setVisibility(View.GONE);
        nullView.setVisibility(View.GONE);
        JuheData.executeWithAPI(getCar91Application().apiIllegalId, queryUrl + "?" + paramsStr, JuheData.POST, new Parameters(), new DataCallBack() {
            @Override
            public void resultLoaded(int err, String reason, String result) {
                loaderView.setVisibility(View.GONE);
                if (err == 0) {
                    try {
                        JSONObject jsonObject = JSONObject.parseObject(result);
                        if (jsonObject.getString("resultcode").equals("200")) {
                            initData(jsonObject.getString("result"));
                        } else {
                            HerilyAlertDialog.Builder builder = new HerilyAlertDialog.Builder(IllegalResultActivity.this);
                            builder.setTitle("对不起");
                            builder.setMessage(jsonObject.getString("reason"));
                            HerilyAlertDialog dialog = builder.create();
                            dialog.setCanceledOnTouchOutside(true);
                            dialog.setOnCancelListener(new OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface arg0) {
                                    finish();
                                }
                            });
                            dialog.show();
                        }
                    } catch (Exception e) {
                        Toast.makeText(IllegalResultActivity.this, "网络异常，请重试", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ((View) resultLstView.getParent()).setVisibility(View.GONE);
                    nullView.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initData(String json) {
        if (RegexUtil.isNotNull(json)) {
            JSONObject obj = JSONObject.parseObject(json);
            String no = obj.getString("hphm");
            JSONArray array = obj.getJSONArray("lists");
            int times = array.size();
            double totalmoney = 0;
            int totalFen = 0;
            List<WeizhangResponseAdapter.WeizhangResponseHistoryJson> weizhangResponseHistoryJsons = new ArrayList<WeizhangResponseAdapter.WeizhangResponseHistoryJson>();
            for (int i = 0; i < times; i++) {
                JSONObject tmp = array.getJSONObject(i);
                String date = tmp.getString("date");
                String area = tmp.getString("area");
                String act = tmp.getString("act");
                String fen = tmp.getString("fen");
                String money = tmp.getString("money");
                String handled = tmp.getString("handled");
                try {
                    if (!"1".equals(handled))
                        totalmoney += Double.parseDouble(money);
                } catch (Exception e) {
                }
                try {
                    totalFen += Integer.parseInt(fen);
                } catch (Exception e) {
                }
                WeizhangResponseHistoryJson weizhangResponseHistoryJson = weizhangResponseAdapter.instanceWeizhangResponseHistoryJson();
                weizhangResponseHistoryJson.setFen(fen);
                weizhangResponseHistoryJson.setInfo(act);
                weizhangResponseHistoryJson.setMoney(money);
                weizhangResponseHistoryJson.setOccur_area(area);
                weizhangResponseHistoryJson.setOccur_date(date);
                weizhangResponseHistoryJsons.add(weizhangResponseHistoryJson);
            }

            weizhangResponseAdapter.setmDate(weizhangResponseHistoryJsons);
            weizhangResponseAdapter.notifyDataSetChanged();
            chepaiView.setText(no);
            descView.setText("共违章" + times + "次，计" + totalFen + "分，罚款" + totalmoney + "元");

            SharedPreferences preferences = getCar91Application().getOtherPreferences();
            String city_name = preferences.getString("city_name", "");
            cityView.setText(city_name);

            if (times > 0) {
                nullView.setVisibility(View.GONE);
                ((View) resultLstView.getParent()).setVisibility(View.VISIBLE);
            } else {
                ((View) resultLstView.getParent()).setVisibility(View.GONE);
                nullView.setVisibility(View.VISIBLE);
            }
        } else {
            ((View) resultLstView.getParent()).setVisibility(View.GONE);
            nullView.setVisibility(View.VISIBLE);
        }
    }

}
